package com.my.all4test.mongodb;

import com.mongodb.MongoClientURI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

@Configuration
@PropertySource("classpath:/com/my/all4test/mongodb/mongodb.properties")
public class MongoConfigurer {
  @Autowired  
  private Environment env; 
  
  public @Bean MongoDbFactory mongoDbFactory() throws Exception {
    MongoClientURI uri = new MongoClientURI(env.getProperty("mongodb.url"));
    return new SimpleMongoDbFactory(uri);
  }
  public @Bean MongoTemplate mongoTemplate() throws Exception {
    return new MongoTemplate(mongoDbFactory());
  }
  
}
